import Tool from  'tool/tool.jsx';
const _req = new Tool();

class Product{

   //获取商品列表
 getProductList(listParam){
   let url = '',
       data = {};
   if(listParam.listType === 'list'){
     url = '/manage/product/list.do';
     data.pageNum = listParam.pageNum;
   }else if(listParam.listType === 'search'){
     url = '/manage/product/search.do';
     data.pageNum =listParam.pageNum;
     data[listParam.searchType] = listParam.searchValue;
   }
  return _req.request({
    type: 'post', 
    url : url,
    data: data
  })
 }
 //获取商品详情
 getProductDetail(productId){
  return _req.request({
    type: 'post', 
    url :'/manage/product/detail.do',
    data:{
      productId : productId || 0
    }
  })
 }
 //调用上架或下架商品接口
 changeProductStatus(productInfo){
  return _req.request({
    type: 'post', 
    url : '/manage/product/set_sale_status.do',
    data: productInfo
  })
 }
  //获取品类列表
  getCategoryList(parentCategoryId){
    return _req.request({
      type: 'post', 
      url : '/manage/category/get_category.do',
      data: {
        categoryId: parentCategoryId || 0
      }
    })
  }
  //验证添加商品
  onCheckProduct(product){
   //$.trim()是jquery方法，用于去除字符串两端的空白字符
   let name = $.trim(product.name),
       subtitle = $.trim(product.subtitle);
   if( typeof name !== 'string' || name.length ===0){
         return{
           status: false,
           msg: '商品名不能为空！'
         }  
    }
   if( typeof subtitle !== 'string' || subtitle.length === 0){
         return{
         status: false,
         msg: '商品描述不能为空！'
         }    
     }
     if( typeof  product.categoryId !== 'number' || !(product.categoryId >0) ){
          return{
          status: false,
          msg: '请选择商品品类！'
          }  
    }  
    if( typeof product.price !== 'number' || !(product.price >= 0)){
      return{
      status: false,
      msg: '请输入正确商品价格！'
      }    
    }
    if( typeof product.stock !== 'number' || !(product.stock >= 0)){
      return{
      status: false,
      msg: '请输入正确商品库存！'
      }    
    }
   if( typeof product.subImages !== 'string' || product.subImages.length === 0){
      return{
      status: false,
      msg: '商品图片不能为空！'
      }    
    }
    if( typeof product.detail !== 'string' || product.detail.length === 0){
      return{
      status: false,
      msg: '商品详情不能为空！'
      }    
    }
      return{
        status: true,
        msg: '验证通过！'
     }
  }
  //保存添加商品
  onSaveProduct(product){
    return _req.request({
      type: 'post', 
      url : '/manage/product/save.do',
      data: product
    })
  }

}
export default Product;